import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from 'typeorm';
import { Category } from './category.entity';

/**
 * 商品实体类
 * 映射到数据库products表，存储商品相关信息
 */
@Entity('products')
export class Product {

  /**
   * 商品ID
   * @type {number}
   * @primaryGeneratedColumn 自动生成
   */
  @PrimaryGeneratedColumn()
  id: number;

  /**
   * 商品名称
   * @type {string}
   * @length 2-100字符
   * @nullable false
   */
  @Column({ length: 100, nullable: false })
  name: string;

  /**
   * 商品描述
   * @type {string}
   * @nullable true
   */
  @Column({ type: 'text', nullable: true })
  description: string;

  /**
   * 商品价格
   * @type {number}
   * @precision 10
   * @scale 2
   * @nullable false
   */
  @Column({ type: 'decimal', precision: 10, scale: 2, nullable: false })
  price: number;

  /**
   * 分类ID
   * @type {number}
   * @nullable false
   */
  @Column({ name: 'category_id', type: 'int', nullable: false })
  categoryId: number;

  /**
   * 分类关联
   * @type {Category}
   * @ManyToOne 多对一关系
   */
  @ManyToOne(() => Category, category => category.products)
  @JoinColumn({ name: 'category_id' })
  category: Category;

  /**
   * 商品图片URL
   * @type {string}
   * @length 1-255字符
   * @nullable true
   */
  @Column({ length: 255, nullable: true })
  imageUrl: string;

  /**
   * 是否激活
   * @type {boolean}
   * @default true
   */
  @Column({ default: true })
  isActive: boolean;

  /**
   * 创建时间
   * @type {Date}
   * @default CURRENT_TIMESTAMP
   */
  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  createdAt: Date;

  /**
   * 更新时间
   * @type {Date}
   * @default CURRENT_TIMESTAMP
   * @onUpdate CURRENT_TIMESTAMP
   */
  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP' })
  updatedAt: Date;
}